﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.Utility.Common;
using System.Data;
using System.Web.Services;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Base
{
    public partial class TeacherTypeList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                classbind();
                typebind();
                SetTypeBind();
            }
        }

        private void classbind()
        {
            this.ddl_FType.DataValueField = "RID";
            this.ddl_FType.DataTextField = "TypeName";
            this.ddl_FType.DataSource = (new BusinessRules.TeacherType()).ListAll();
            this.ddl_FType.DataBind();
            ListItem item1 = new ListItem();
            item1.Value = "0";
            item1.Text = "尚未分类";
            this.ddl_FType.Items.Add(item1);
            ListItem item2 = new ListItem();
            item2.Value = "-1";
            item2.Text = "----全部----";
            this.ddl_FType.Items.Add(item2);
            this.ddl_FType.SelectedValue = "-1";
        }

        private void typebind()
        {
            DataTable dt = new DataTable();
            Common.IListToTable(dt, new BusinessRules.ListTeacher());
            IList<BusinessRules.ListTeacher> mylist = (new BusinessRules.TeacherType()).ListTeacher(int.Parse(this.ddl_FType.SelectedValue), this.txt_TeacherCode.Text, this.txt_TeacherName.Text);
            for (int i = 0; i < mylist.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["TeacherID"] = mylist[i].TeacherID;
                row["TeachCode"] = mylist[i].TeachCode;
                row["TeachName"] = mylist[i].TeachName;
                row["TypeName"] = mylist[i].TypeName;
                row["Sexy"] = mylist[i].Sexy;
                row["Birthday"] = mylist[i].Birthday;
                dt.Rows.Add(row);
            }
            
            //------------------------------------------------------------------------分页处理开始......
            int PageNow = 0;
            DataView dv = dt.DefaultView;
            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dv;
            ps.AllowPaging = true;
            ps.PageSize = 20;
            if (this.txt_Go.Text.ToString() == "")
                this.txt_Go.Text = "1";
            if (this.txt_Go.Text.ToString() == "0")
                this.txt_Go.Text = "1";
            if (Convert.ToInt32(this.txt_Go.Text.ToString()) >= ps.PageCount)
                this.txt_Go.Text = ps.PageCount.ToString();
            PageNow = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //this.lbn_Up.Enabled = true;
            //this.lbn_Next.Enabled = true;
            //if (PageNow == 0)
            //{
            //    this.lbn_Up.Enabled = false;
            //}
            //if (PageNow == ps.PageCount - 1)
            //{
            //    this.lbn_Next.Enabled = false;
            //}
            ps.CurrentPageIndex = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //-------------------------------------------------------------------------分页处理结束

            this.rpt_TeacherType.DataSource = ps;
            this.rpt_TeacherType.DataBind();

            //------------------------------------------------------------------------记录总数、当前页数、总页数处理开始......
            //this.lbl_SumCount.Text = ds.Tables[0].Rows.Count.ToString();
            this.lbl_ToPage.Text = this.txt_Go.Text.ToString();
            this.lbl_PageCount.Text = ps.PageCount.ToString();
            
            //------------------------------------------------------------------------------记录总数、当前页数、总页数处理结束
        }
        
        protected void btnBind_Click(object sender, EventArgs e)
        {
            typebind();
        }

        private void SetTypeBind()
        {
            this.ddl_SetFType.DataValueField = "RID";
            this.ddl_SetFType.DataTextField = "TypeName";
            this.ddl_SetFType.DataSource = (new BusinessRules.TeacherType()).ListAll();
            this.ddl_SetFType.DataBind();
            ListItem item1 = new ListItem();
            item1.Value = "0";
            item1.Text = "尚未分类";
            this.ddl_SetFType.Items.Add(item1);
        }

        [System.Web.Services.WebMethod]
        public static string CheckBoxList(int TeacherID,string SumID)
        {
            string result = SumID;
            if (result.IndexOf(TeacherID.ToString() + "|") > -1)
                result = result.Replace(TeacherID.ToString() + "|", "");
            else
            {
                result = result + TeacherID.ToString() + "|";
            }
            return result;
        }

        [System.Web.Services.WebMethod]
        public static bool ChangeFType(string SumID, string ftype)
        {
            bool result = true;
            string msg = string.Empty;
            BusinessRules.TeacherRelation bll = new BusinessRules.TeacherRelation();
            
            string [] teacher = Common.DelStrLast(SumID,"|").Split('|');
            for (int i = 0; i < teacher.Count(); i++)
            {
                Entities.t_TeacherRelation model = new Entities.t_TeacherRelation();
                model.TeacherID = int.Parse(teacher[i]);
                model.TypeID = int.Parse(ftype);
                model.SchoolMark = SchoolMark;
                result = bll.Add(model, ref msg);
            }
            return result;
        }

        public string GetCbxState(string TeacherID)
        {
            //update by lxy 20100916 修改QC37号bug

            //if (this.hdf_SumID.Value.Contains(TeacherID + "|"))
            //{
            //    return "checked='checked'";
            //}
            //else
            //{
                return "";
            //}
        }
    }
}